define(['app'], function (app) {
	'use strict';

	var controller = function controller($scope, Seller_send, $stateParams, GoDialog, GoPage) {

		$scope.$on('$ionicView.afterEnter', function () {
			GoPage.init_top($scope, 70, 0);
			$scope.init_data();
		}, false);

		$scope.order_id = $stateParams.order_id;//订单号
		$scope.buttonId = $stateParams.buttonId;//发货按钮跳转过来时，要显示发货相关的输入框。发货按钮对应的buttonId为4。
		$scope.form = new Object();

		$scope.form.show_status = "";

		$scope.initialization = function () {
			$scope.form.send = new Array();//以obj_id作为键名
			$scope.form.deladdress = "";//收货地址
			$scope.form.deladdress_select = { id: 0, addr: "请选择收货地址" };
			$scope.form.logi_id = 0;//物流id
			$scope.form.logi_no = "";//物流单号
			$scope.form.logi_select = { id: 0, comp_name: "请选择快递公司" };
			$scope.logisticscomp = new Array();//快递公司列表
			//			$scope.form.num = new Array();//获取页面输入的发货数量， 不提交
		}
		$scope.initialization();//初始化参数
		$scope.doSelectComp = function () {
			//id是值 name是显示
			GoDialog.showSelect("请选择快递公司", $scope.logisticscomp, $scope.form.logi_select, function (data) {
				$scope.form.logi_id = data.id;
				$scope.form.logi_select = data;
			});
		}
		$scope.doSelectAddress = function () {
			//id是值 name是显示
			var list = new Array();
			$scope.addressList.forEach(obj => {
				if (obj.id == undefined)
					obj.id = obj.addr_id;
				if (obj.name)
					obj.name = undefined;
				list.push(obj);
			});
			GoDialog.showSelect("请选择收货地址", list, $scope.form.deladdress_select, function (data) {
				$scope.form.deladdress = data.id;
				$scope.form.deladdress_select = data;
			});
		}

		$scope.init_data = function () {
			Seller_send.get_order($scope.order_id, function (data) {
				for (var i = 0; i < data.order_objects.length; i++) {//先转整型
					data.order_objects[i].delivery = parseInt(data.order_objects[i].delivery);
					data.order_objects[i].count = parseInt(data.order_objects[i].count);
				}
				$scope.order = data;
				console.log(data)
				var status = data.status;
				switch (status) {
					case 'finish':
						$scope.form.show_status = "已完成 ";
						break;
					case 'dead':
						$scope.form.show_status = "已取消 ";
						break;
					case 'active':
						if (data.ship_status == 0) {
							$scope.form.show_status = "待发货 ";
						}
						if (data.ship_status == 1) {
							$scope.form.show_status = "已发货 ";
						}
						break;
				}
			});
			//获取快递公司
			Seller_send.get_logisticscomp(function (data) {
				for (var i = 0; i < data.page.count; i++) {
					if (data.logisticscomp[i].choosen_flg) {
						$scope.form.logi_id = data.logisticscomp[i].id;
						$scope.logisticscomp.push(data.logisticscomp[i]);
					}
				}
			});
			//获取发货地址
			Seller_send.getAddressList(function (data) {
				$scope.addressList = data.addr;
				for (var i = 0; i < data.addr.length; i++) {//默认显示默认的发货地址
					if (data.addr[i].def_send) {
						$scope.form.deladdress = data.addr[i].addr_id;//收货地址
						break;
					}
				}
			});
		}
		//$scope.init_data(); //必须在定义之后

		//页面跳转
		$scope.onshow_first = function (store_id) {
			GoPage.go_states("first", { "store_id": store_id, "index": 0 });
		}
		$scope.onshow_productdetail = function (goods_id) {
			GoPage.go_states("productdetail", { "goods_id": goods_id, "type": "goods" });
		}

		//确认发货
		$scope.confirmSend = function () {
			var sendNumInputs = document.getElementsByClassName("sendNumInput");//输入框的个数sendNumInputs.length
			$scope.isLegal = true;//输入是否合法
			$scope.form.send.splice(0, $scope.form.send.length);//必须先清空
			for (var i = 0; i < $scope.order.order_objects.length; i++) {//判断是否输入了发货数量，至少输入一个商品的发货数量且输入的数量必须合法
				if (sendNumInputs[i].value) {//有值
					$scope.mySend = new Object();
					//					console.log($scope.order.order_objects[i])
					$scope.obj_id = $scope.order.order_objects[i].item_id;//obj_id，现在改用item_id
					$scope.count = $scope.order.order_objects[i].count;//购买数量，发货数量必须小于等于购买数量
					$scope.delivery = $scope.order.order_objects[i].delivery;//已经发货的数量
					$scope.currentQuantity = sendNumInputs[i].value;//目前输入的发货数量
					//					console.log("购买："+ $scope.count)
					//					console.log("已发货："+ $scope.delivery)
					//					console.log("准备发货："+ $scope.currentQuantity)
					if ($scope.count < parseInt($scope.currentQuantity) + parseInt($scope.delivery)) {//转一下
						$scope.isLegal = false;
						break;
					} else {
						$scope.mySend[$scope.obj_id] = $scope.currentQuantity;
						$scope.form.send.push($scope.mySend);
					}
				}
			}
			//			console.log($scope.form.send)
			if (!$scope.isLegal) {
				GoDialog.toast("发货总数量必须小于等于购买总数量！");
			} else if ($scope.form.send.length == 0) {
				GoDialog.toast("请至少输入一个商品的发货数量！");
			} else if (!$scope.form.logi_id) {
				GoDialog.toast("请选择快递公司！");
			} else if (!$scope.form.logi_no) {
				GoDialog.toast("请输入物流单号！");
			} else {
				Seller_send.confirmSend($scope.order_id, $scope.form.send, $scope.form.deladdress, $scope.form.logi_id, $scope.form.logi_no, function (data) {
					if (data.success) {
						GoDialog.toast("发货成功！");
						$scope.initialization();//初始化参数
						GoPage.go_states("afallseller");
					} else {
						GoDialog.toast(data.msg);
					}
				});
			}
		}
	}

	controller.$inject = ['$scope', 'Seller_send', '$stateParams', 'GoDialog', 'GoPage'];

	return app.registerController("seller_sendctrl", controller);
});